package com.zqz.webserver.herocat;

import com.zqz.webserver.servlet.HeroRequest;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.QueryStringDecoder;

import java.util.List;
import java.util.Map;

public class HttpCustomRequest implements HeroRequest {

    private final HttpRequest httpRequest;

    public HttpCustomRequest(HttpRequest httpRequest) {
        this.httpRequest = httpRequest;
    }

    private QueryStringDecoder getDecoder() {
        return new QueryStringDecoder(this.getUri());
    }

    @Override
    public String getUri() {
        return httpRequest.uri();
    }

    @Override
    public String getPath() {
        return this.getDecoder().path();
    }

    @Override
    public String getMethod() {
        return httpRequest.method().name();
    }

    @Override
    public Map<String, List<String>> getParameters() {
        return this.getDecoder().parameters();
    }

    @Override
    public List<String> getParameters(String name) {
        return this.getParameters().get(name);
    }

    @Override
    public String getParameter(String name) {
        List<String> parameters = this.getParameters(name);

        if (null == parameters || parameters.isEmpty()) {
            return null;
        }
        return parameters.get(0);
    }
}
